//
//  ActivityViewController.m
//  框架
//
//  Created by 这是工作本 on 15/12/25.
//  Copyright © 2015年 这是工作本. All rights reserved.
//

#import "ActivityViewController.h"

@interface ActivityViewController () <UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UISearchBar *searchBar;

@property (nonatomic, retain) UIButton *sport;

@property (nonatomic, retain) UIButton *area;

@property (nonatomic, retain) UIButton *activity;

@property (nonatomic, retain) UIButton *arrange;

@property (nonatomic, retain) UIView *viewB;

@property (nonatomic, assign) BOOL click;

@property (nonatomic, retain) UITableView *tableButton;

@property (nonatomic, retain) UITableView *tableV;

@property (nonatomic, retain) UIImageView *sportV;

@property (nonatomic, retain) UIImageView *viewV;

@property (strong,nonatomic) MBProgressHUD * hud;

@property (nonatomic, retain) UIButton *like;

@property (nonatomic, retain) NSMutableArray *activityArr;

@property (nonatomic, retain) NSMutableArray *detailActivityArr;
@end

@implementation ActivityViewController



//设置需要隐藏的控件
- (void)viewWillDisappear:(BOOL)animated
{
    self.searchBar.hidden = YES;
    self.viewV.hidden = YES;
    [self.searchBar resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.searchBar.hidden = NO;
    self.viewV.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;

}

- (NSMutableArray *)activityArr
{
    if (nil == _activityArr) {
        
        self.activityArr = [NSMutableArray array];
    }
    return _activityArr;
}

- (NSMutableArray *)detailActivityArr
{
    if (nil == _detailActivityArr) {
        
        self.detailActivityArr = [NSMutableArray array];
    }
    return _detailActivityArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.activityArr = [NSMutableArray array];

    self.view.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];

    [self getNetData];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:29 / 255.0 green:127 / 255.0  blue:140 / 255.0  alpha:1.0];
    
    //点击出详情
    self.click = YES;
    
    //城市
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"北京" style:UIBarButtonItemStylePlain target:self action:@selector(left)];
    
    //设置字体颜色大小
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                   [UIFont fontWithName:@"Helvetica" size:16.0 * ScreenWidth], NSFontAttributeName, [UIColor colorWithRed:255 / 255.0 green:255 / 255.0  blue:255 / 255.0  alpha:1.0], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    
    //消息
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"椭圆-17@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right)];
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    
    // 显示正在网络请求
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.labelText = @"Loading";
    //改变了加载Loading的样式
    
    self.hud.labelFont = [UIFont fontWithName:@"American Typewriter" size:16 * ScreenWidth];
    [self.hud show:YES];


    //搜索
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(75 * ScreenWidth, 10, 270 * ScreenWidth, 30 * ScreenHeigth)];
    
    self.searchBar.delegate = self;
    self.searchBar.placeholder = @"搜索";
    self.searchBar.tintColor = [UIColor colorWithRed:207 / 255.0 green:146 / 255.0 blue:19 / 255.0 alpha:1.0];
    self.searchBar.showsScopeBar = YES;
    [self.navigationController.navigationBar addSubview:self.searchBar];
    
    //点击空白回收键盘
    //    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction)];
    //    [self.view addGestureRecognizer:ges];
    
    
    //检索
    //运动项目
    
    self.sport = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sport.frame = CGRectMake(0, 64 , 104 * ScreenWidth, 44 * ScreenHeigth);
    self.sport.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.sport setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.sport.font = [UIFont systemFontOfSize:13.0 * ScreenWidth];
    [self.sport setTitle:@"运动项目" forState:UIControlStateNormal];
    [self.sport addTarget:self action:@selector(sportAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sport];
    
    //箭头
    
    self.sportV = [[UIImageView alloc] initWithFrame:CGRectMake(80 * ScreenWidth, 64 + 18 * ScreenHeigth, 10 * ScreenWidth, 10 * ScreenHeigth)];
    self.sportV.image = [UIImage imageNamed:@"003.png"];
    [self.view addSubview:self.sportV];
    
    //分割线
    UIImageView *divide = [[UIImageView alloc] initWithFrame:CGRectMake(self.sport.frame.origin.x + self.sport.frame.size.width + 1 * ScreenWidth, 70 * ScreenHeigth, 1 * ScreenWidth, 30 * ScreenHeigth)];
    divide.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:35 / 255.0 blue:35 / 255.0 alpha:1.0];
    [self.view addSubview:divide];
    
    //地区活动
    
    self.area = [UIButton buttonWithType:UIButtonTypeCustom];
    self.area.frame = CGRectMake(divide.frame.origin.x + divide.frame.size.width, 64 , self.sport.frame.size.width, 44 * ScreenHeigth);
    self.area.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.area setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.area.font = [UIFont systemFontOfSize:13.0 * ScreenWidth];
    [self.area setTitle:@"地区范围" forState:UIControlStateNormal];
    [self.area addTarget:self action:@selector(sportAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.area];
    
    //箭头
    
    UIImageView *area = [[UIImageView alloc] initWithFrame:CGRectMake(188 * ScreenWidth, 64 + 18 * ScreenHeigth, 10 * ScreenWidth, 10 * ScreenHeigth)];
    area.image = [UIImage imageNamed:@"003.png"];
    [self.view addSubview:area];
    
    //分割线
    UIImageView *divideA = [[UIImageView alloc] initWithFrame:CGRectMake(self.area.frame.origin.x + self.area.frame.size.width + 1 * ScreenWidth, 70 * ScreenHeigth, 1 * ScreenWidth, 30 * ScreenHeigth)];
    divideA.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:35 / 255.0 blue:35 / 255.0 alpha:1.0];
    [self.view addSubview:divideA];
    
    
    //优惠活动
    
    self.activity = [UIButton buttonWithType:UIButtonTypeCustom];
    self.activity.frame = CGRectMake(divideA.frame.origin.x + divideA.frame.size.width, 64 , self.sport.frame.size.width, 44 * ScreenHeigth);
    self.activity.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.activity setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.activity.font = [UIFont systemFontOfSize:13.0 * ScreenWidth];
    [self.activity setTitle:@"优惠活动" forState:UIControlStateNormal];
    [self.activity addTarget:self action:@selector(sportAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.activity];
    
    //箭头
    
    UIImageView *activity = [[UIImageView alloc] initWithFrame:CGRectMake(295 * ScreenWidth, 64 + 18 * ScreenHeigth, 10 * ScreenWidth, 10 * ScreenHeigth)];
    activity.image = [UIImage imageNamed:@"003.png"];
    [self.view addSubview:activity];
    
    
    
    //分割线
    UIImageView *divideT = [[UIImageView alloc] initWithFrame:CGRectMake(self.activity.frame.origin.x + self.activity.frame.size.width + 1 * ScreenWidth, 70 * ScreenHeigth, 1 * ScreenWidth, 30 * ScreenHeigth)];
    divideT.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:35 / 255.0 blue:35 / 255.0 alpha:1.0];
    [self.view addSubview:divideT];
    
    
    //排列方式
    
    self.arrange = [UIButton buttonWithType:UIButtonTypeCustom];
    self.arrange.frame = CGRectMake(divideT.frame.origin.x + divideT.frame.size.width, 64, self.sport.frame.size.width, 44 * ScreenHeigth);
    self.arrange.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.arrange setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.arrange.font = [UIFont systemFontOfSize:13.0 * ScreenWidth];
    [self.arrange setTitle:@"排列方式" forState:UIControlStateNormal];
    [self.arrange addTarget:self action:@selector(sportAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.arrange];
    
    //箭头
    
    UIImageView *arrange = [[UIImageView alloc] initWithFrame:CGRectMake(400 * ScreenWidth, 64 + 18 * ScreenHeigth, 10 * ScreenWidth, 10 * ScreenHeigth)];
    arrange.image = [UIImage imageNamed:@"003.png"];
    [self.view addSubview:arrange];
    
    
    //检索详情
    self.viewB = [[UIView alloc] initWithFrame:CGRectMake(0, self.sport.frame.origin.y + self.sport.frame.size.height, [UIScreen mainScreen].bounds.size.width, 160 * ScreenHeigth)];
    self.viewB.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
    [self.view addSubview:self.viewB];
    
    [self.viewB removeFromSuperview];
    
    //详情按钮
    
    self.tableButton = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.viewB.frame.size.height) style:UITableViewStylePlain];
    self.tableButton.dataSource = self;
    self.tableButton.delegate = self;
    self.tableButton.backgroundColor = [UIColor colorWithRed:19 / 255.0 green:19 / 255.0 blue:19 / 255.0 alpha:1.0];
    self.tableButton.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableButton registerClass:[ChoiceTableViewCell class] forCellReuseIdentifier:@"cellchoice"];
    [self.viewB addSubview:self.tableButton];
    
    [self.tableButton removeFromSuperview];
    
    
    //场馆界面
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, self.sport.frame.origin.y + self.sport.frame.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - self.sport.frame.origin.y - self.sport.frame.size.height - 50) style:UITableViewStylePlain];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    self.tableV.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    self.tableV.separatorColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
    [self.tableV registerClass:[GymTableViewCell class] forCellReuseIdentifier:@"cellGym"];
    [self.view addSubview:self.tableV];
    
    //解决左边空白问题
    if ([self.tableV respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableV setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableV respondsToSelector:@selector(setLayoutMargins:)])
    {
        [self.tableV setLayoutMargins:UIEdgeInsetsZero];
    }
    
    
    
    
    
    
}


-(void)getNetData
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
//    NSString *lat = [NSString stringWithFormat:@"%f", self.lat];
//    NSString *lng = [NSString stringWithFormat:@"%f", self.lng];
    
    NSDictionary *dict = @{@"lat":@"39.9819100000", @"lng":@"116.4831220000"};
    
    [manager POST:@"http://192.168.1.157:8080/api/center?userId=3339WrRIAYfv9wPeGqC&equipment=ios&requestType=Venue_List_Api&token=74214cd04c081efd29a1f55d3d7b6c3a" parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {

        NSArray *activity = [responseObject objectForKey:@"data"];
        for (NSDictionary * dic in activity) {
            
            VenuesList *list = [[VenuesList alloc] init];

            [list setValuesForKeysWithDictionary:dic];
            [self.activityArr addObject:list];
            
            
        }
        
        [self.tableV reloadData];
        
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
    }];

    
}

//代理方法解决15像素空白问题

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (void)sportAction:(UIButton *)sender
{
    
    
    if (YES == self.click) {
        [self.view addSubview:self.viewB];
        [self.viewB addSubview:self.tableButton];
        self.click = NO;
        
        
    } else
    {
        [self.viewB removeFromSuperview];
        self.click = YES;
    }
    
    
    
    
}

//回收键盘
- (void)gesAction
{
    [self.searchBar resignFirstResponder];
    
    
}


//提示框
- (void)left
{
    
    UIAlertView *alertv = [[UIAlertView alloc] initWithTitle:nil message:@"暂未开放其他城市" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:alertv repeats:YES];
    [alertv show];
    
}

- (void)timerAction:(NSTimer *)send
{
    UIAlertView *aler = (UIAlertView *)[send userInfo];
    [aler dismissWithClickedButtonIndex:0 animated:NO];
    aler = NULL;
}

- (void)right
{
    

    
//    
//    ChatViewController *chat = [[ChatViewController alloc] init];
//    [self.navigationController pushViewController:chat animated:YES];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.tableButton == tableView) {
        
        return 6;
    } else
    {
        return self.activityArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.tableButton == tableView) {
        
        ChoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellchoice"];
        cell.textLabel.text = @"篮球";
        cell.font = [UIFont systemFontOfSize:13.0 * ScreenWidth];
        cell.textLabel.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
        cell.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
        
        return cell;
        
        
    } else
    {
        
        GymTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellGym"];
        ;
        cell.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        VenuesList *list = [self.activityArr objectAtIndex:indexPath.row];
        
        [cell cellForData:list];
        
        return cell;
    }
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //添加水波动画
    CATransition *ansition = [CATransition animation];
    ansition.duration = 1.0;
    ansition.type = @"rippleEffect";
    [self.view.window.layer addAnimation:ansition forKey:nil];
    
    DetailActivityViewController *detail = [[DetailActivityViewController alloc] init];

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    VenuesList *list = [self.activityArr objectAtIndex:indexPath.row];
    NSString *projectId = [NSString stringWithFormat:@"%ld", list.projectId];
    //传场馆id
    detail.modelId = list.iden;
    
    NSDictionary *dict = @{@"id":list.iden, @"projectId":projectId};
    
    [manager POST:@"http://192.168.1.157:8080/api/center?userId=3339WrRIAYfv9wPeGqC&equipment=ios&requestType=View_Venue_Api&token=74214cd04c081efd29a1f55d3d7b6c3a" parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
      
        NSArray *project = [responseObject objectForKey:@"projects"];
        for (NSDictionary *dic in project) {
            
            detailVenuesList *de = [[detailVenuesList alloc] init];
            [de setValuesForKeysWithDictionary:dic];
            detail.projectId = de.iden;
            [self.detailActivityArr addObject:de];
            
        }
      
            [self.tableV reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        
    }];

    
//    detailVenuesList *proId = [self.detailActivityArr objectAtIndex:indexPath.row];
//    detail.projectId = proId.iden;
    
    
    [self.navigationController pushViewController:detail animated:YES];
    
    
    
    
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableButton == tableView) {
        
        return 30 * ScreenHeigth;
    } else
    {
        return 65 * ScreenHeigth;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
